﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using TodoUser.Data;
using TodoUser.Data.Models;
using TodoUser.Models;
using TodoUser.Adapters.Interfaces;
using TodoUser.Adapters;
using Microsoft.AspNet.Identity;
using TodoUser.Adapters.Adapters;

namespace TodoUser.AppServices
{
    public class TodoServiceController : ApiController
    {

        IUserAdapter _adapter;

        public TodoServiceController()
        {
            _adapter = new UserAdapter();
        }

        public TodoServiceController(IUserAdapter apd) {
            _adapter = apd;
        }


        [Route("all")]
        public IHttpActionResult GetAllTodos()
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {

                List<TodoIndexVm> list = new List<TodoIndexVm>();
                list = db.Todos.Select(t => new TodoIndexVm
                {
                    TodoId = t.TodoId,
                    Task = t.Task,
                    TimeAdded = t.TimeAdded,
                    UserId = t.UserId,
                    TimeCompleted = t.TimeCompleted

                }).ToList();

                return Ok(list);

            }
            return Ok("Error");
        }
        //
        //[Route("all/{id}")]
        [Route("allid")]
        public IHttpActionResult GetTodosById()
        {

            //get Id
            string cId = User.Identity.GetUserId();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                List<TodoIndexVm> list = new List<TodoIndexVm>();
                list = db.Todos
                    .Where(t => t.UserId == cId)
                    .Select(t => new TodoIndexVm
                    {
                        TodoId = t.TodoId,
                        Task = t.Task,
                        TimeAdded = t.TimeAdded,
                        UserId = t.UserId,
                        TimeCompleted = t.TimeCompleted

                    })
                    .ToList();

                return Ok(list);

            }
            return Ok("Error");
        }

        //[Authorize]
        [Route("GetInfo")]
        public IHttpActionResult GetUserInfo()
        {
            string userType = "User";
            
            if (User.IsInRole("Admin"))
            {
                userType = "Admin";
            }
            return Ok(userType);
        }
        //user.IsInRole("Admin")
    }
}